/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmItemBrowseDialog1.java
 *
 * Created on Apr 6, 2011, 10:50:31 AM
 */
package inventorymanagement;

import javax.swing.JOptionPane;
import DataAccess.Item;
import DataAccess.TableAccess;
import interact.iCheckForm;
import java.awt.Dimension;
import java.awt.Toolkit;
import model.ColorTable;

/**
 *
 * @author Home
 */
public class frmItemBrowseDialog1 extends javax.swing.JDialog {

    static frmProcedureBill code;

    /** Creates new form frmItemBrowseDialog1 */
    public frmItemBrowseDialog1(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        this.setLocation(((int) screenSize.getWidth() - this.getWidth()) / 2,
                ((int) screenSize.getHeight() - this.getHeight()) / 2);
        try {
            DataAccess.TableAccess.readToCombo("select * from Categories", cboCategory, "CategoryName");
        } catch (Exception e) {
            e.printStackTrace();
        }
        this.refresh();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        txtItemID = new javax.swing.JTextField();
        txtItemName = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        cboCategory = new javax.swing.JComboBox();
        btnRefresh = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblItems = new ColorTable();
        btnSelect = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Items Browse");

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder(new java.awt.Color(51, 153, 255), null));

        jLabel2.setText("Item ID:");

        txtItemID.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtItemIDKeyReleased(evt);
            }
        });

        txtItemName.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtItemNameKeyReleased(evt);
            }
        });

        jLabel3.setText("Item Name:");

        jLabel4.setText("Category:");

        cboCategory.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "All" }));
        cboCategory.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cboCategoryItemStateChanged(evt);
            }
        });

        btnRefresh.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Symbol-Refresh.png"))); // NOI18N
        btnRefresh.setText("Refresh");
        btnRefresh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRefreshActionPerformed(evt);
            }
        });

        tblItems.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        tblItems.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblItemsMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblItems);

        btnSelect.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Symbol-Check.png"))); // NOI18N
        btnSelect.setText("Select");
        btnSelect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSelectActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtItemID, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtItemName))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 15, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cboCategory, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(btnRefresh)))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE)
                    .addComponent(btnSelect))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtItemID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(cboCategory, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtItemName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(btnRefresh))
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnSelect)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tblItemsMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblItemsMouseClicked
        // TODO add your handling code here:
        int row = tblItems.getSelectedRow();
        String str = tblItems.getValueAt(row, 0).toString();
        String str1 = tblItems.getValueAt(row, 1).toString();
        txtItemID.setText(str);
        txtItemName.setText(str1);
}//GEN-LAST:event_tblItemsMouseClicked

    private void btnSelectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSelectActionPerformed
        int itemID = tblItems.getSelectedRow();
        if (itemID == -1) {
            JOptionPane.showMessageDialog(this, "You must choose a item");
            return;
        } else {
            int row = tblItems.getSelectedRow();
            code.txtItemID.setText(tblItems.getValueAt(row, 0).toString());
            this.dispose();
        }
}//GEN-LAST:event_btnSelectActionPerformed

    private void txtItemIDKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtItemIDKeyReleased
        // TODO add your handling code here:
        String str = txtItemID.getText();
        String sql = "";
        System.out.println(cboCategory.getSelectedIndex());
        if (iCheckForm.checkNumber(str)) {
            JOptionPane.showMessageDialog(this, "CustomerID Invalid Format", "ERROR", JOptionPane.WARNING_MESSAGE);
            txtItemID.setText("");
            return;
        }
        if (str.equals("")) {
            if (cboCategory.getSelectedIndex() == 0) {
                //sql = "select * from Items";
                sql = "select Items.ItemID, Categories.CategoryName, Items.ItemName, Items.QuantityAvailable,"
                        + " Items.Price, Items.Unit, Items.Descript, Items.Manufacture, Items.Status from Items INNER JOIN Categories "
                        + "ON Items.CategoryID = Categories.CategoryID ";
            } else {
                sql = "select Items.ItemID, Categories.CategoryName, Items.ItemName, Items.QuantityAvailable,"
                        + " Items.Price, Items.Unit, Items.Descript, Items.Manufacture, Items.Status from Items INNER JOIN Categories "
                        + "ON Items.CategoryID = Categories.CategoryID "
                        + "where Items.CategoryID=" + cboCategory.getSelectedIndex();
            }
        } else {
            if (cboCategory.getSelectedIndex() == 0) {
                sql = "select Items.ItemID, Categories.CategoryName, Items.ItemName, Items.QuantityAvailable,"
                        + " Items.Price, Items.Unit, Items.Descript, Items.Manufacture, Items.Status from Items INNER JOIN Categories "
                        + "ON Items.CategoryID = Categories.CategoryID where ItemID=" + Integer.parseInt(str);

            } else {
                sql = "select Items.ItemID, Categories.CategoryName, Items.ItemName, Items.QuantityAvailable,"
                        + " Items.Price, Items.Unit, Items.Descript, Items.Manufacture, Items.Status from Items INNER JOIN Categories "
                        + "ON Items.CategoryID = Categories.CategoryID "
                        + "where ItemID='" + Integer.parseInt(str) + "' and Items.CategoryID=" + cboCategory.getSelectedIndex();
            }
        }


        try {
            DataAccess.Item.readToTableItem(sql, tblItems);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }//GEN-LAST:event_txtItemIDKeyReleased

    private void txtItemNameKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtItemNameKeyReleased
        // TODO add your handling code here:
        String str = txtItemName.getText();
        String sql = "";
        if (str.equals("")) {
            if (cboCategory.getSelectedIndex() == 0) {
                //sql = "select * from Items";
                sql = "select Items.ItemID, Categories.CategoryName, Items.ItemName, Items.QuantityAvailable,"
                        + " Items.Price, Items.Unit, Items.Descript, Items.Manufacture, Items.Status from Items INNER JOIN Categories "
                        + "ON Items.CategoryID = Categories.CategoryID ";
            } else {
                sql = "select Items.ItemID, Categories.CategoryName, Items.ItemName, Items.QuantityAvailable,"
                        + " Items.Price, Items.Unit, Items.Descript, Items.Manufacture, Items.Status from Items INNER JOIN Categories "
                        + "ON Items.CategoryID = Categories.CategoryID "
                        + "where Items.CategoryID=" + cboCategory.getSelectedIndex();
            }
        } else {
            if (cboCategory.getSelectedIndex() == 0) {
                sql = "select Items.ItemID, Categories.CategoryName, Items.ItemName, Items.QuantityAvailable,"
                        + " Items.Price, Items.Unit, Items.Descript, Items.Manufacture, Items.Status from Items INNER JOIN Categories "
                        + "ON Items.CategoryID = Categories.CategoryID "
                        + "and ItemName like '%" + str + "%'";
            } else {
                sql = "select Items.ItemID, Categories.CategoryName, Items.ItemName, Items.QuantityAvailable,"
                        + " Items.Price, Items.Unit, Items.Descript, Items.Manufacture, Items.Status from Items INNER JOIN Categories "
                        + "ON Items.CategoryID = Categories.CategoryID "
                        + "where Items.CategoryID='" + cboCategory.getSelectedIndex() + "' "
                        + "and ItemName like '%" + str + "%'";
            }
        }

        try {
            DataAccess.Item.readToTableItem(sql, tblItems);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }//GEN-LAST:event_txtItemNameKeyReleased

    private void btnRefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRefreshActionPerformed
        // TODO add your handling code here:
        this.refresh();
        this.resetForm();
    }//GEN-LAST:event_btnRefreshActionPerformed

    private void cboCategoryItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cboCategoryItemStateChanged
        // TODO add your handling code here:
        if (cboCategory.getSelectedIndex() == 0) {
            try {
                DataAccess.Item.readToTableItem("select Items.ItemID, Categories.CategoryName, Items.ItemName, Items.QuantityAvailable,"
                        + " Items.Price, Items.Unit, Items.Descript, Items.Manufacture, Items.Status from Items INNER JOIN Categories "
                        + "ON Items.CategoryID = Categories.CategoryID ", tblItems);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            try {
                DataAccess.Item.readToTableItem("select Items.ItemID, Categories.CategoryName, Items.ItemName, Items.QuantityAvailable,"
                        + " Items.Price, Items.Unit, Items.Descript, Items.Manufacture, Items.Status from Items INNER JOIN Categories "
                        + "ON Items.CategoryID = Categories.CategoryID "
                        + "where Items.CategoryID=" + cboCategory.getSelectedIndex(), tblItems);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }//GEN-LAST:event_cboCategoryItemStateChanged

    private void refresh() {
        try {
            DataAccess.Item.readToTableItem("select Items.ItemID, Categories.CategoryName, Items.ItemName, Items.QuantityAvailable,"
                    + " Items.Price, Items.Unit, Items.Descript, Items.Manufacture, Items.Status from Items INNER JOIN Categories "
                    + "ON Items.CategoryID = Categories.CategoryID ", tblItems);
            //DataAccess.TableAccess.readToCombo("select * from Categories", cboItemCategory, "CategoryName");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void resetForm() {
        txtItemID.setText("");
        txtItemName.setText("");
        //txtItemPrice.setText("");
        cboCategory.setSelectedIndex(0);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                frmItemBrowseDialog1 dialog = new frmItemBrowseDialog1(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnRefresh;
    private javax.swing.JButton btnSelect;
    private javax.swing.JComboBox cboCategory;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblItems;
    private javax.swing.JTextField txtItemID;
    private javax.swing.JTextField txtItemName;
    // End of variables declaration//GEN-END:variables
}
